package com.useeinfo.framework.sugar.tools;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by sillyboy on 2017/9/4
 */
public class AnalysisObjectList {
    private final static Logger logger = LoggerFactory.getLogger(AnalysisObjectList.class);

    public static String formatDouble(double d) {
        DecimalFormat df = new DecimalFormat("########0.00");
        return df.format(d);
    }

    public static String formatDouble3(double d) {
        DecimalFormat df = new DecimalFormat("########0.000");
        return df.format(d);
    }

    public static Double formatDoubleToDouble(double d) {
        DecimalFormat df = new DecimalFormat("########0.00");
        return StringConverters.ToDouble(df.format(d));
    }

    public static JSONObject mapGetJsonObject(Map<String, JSONObject> map, String key) {
        try {
            return map.get(key);
        } catch (Exception ignored) {
        }
        return new JSONObject();
    }

    public static String getJsonObjectString(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getString(key);
        } catch (Exception ignored) {
        }
        return "";
    }

    public static JSONArray getJsonArray(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONArray(key);
        } catch (Exception ignored) {
        }
        return null;
    }


    public static JSONObject getJsonObject(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONObject(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static String ObjectString(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getString(key);
        } catch (Exception ignored) {
        }
        return "";
    }

    public static Long ObjectLong(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getLong(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static Double ObjectDouble(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getDouble(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static Integer ObjectInteger(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getInt(key);
        } catch (Exception ignored) {
        }
        return null;
    }

    public static String getJsonString(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? "" : ja.getString(index);
    }

    public static Long getJsonLong(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0L : ja.getLong(index);
    }

    public static Integer getJsonInteger(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0 : ja.getInt(index);
    }

    public static Double getJsonDouble(JSONArray ja, Integer index) {
        return "null".equals(ja.getString(index)) ? 0D : ja.getDouble(index);
    }

    public static Boolean getJsonToBoolean(JSONArray ja, Integer index) {
        String result = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        if ("true".equals(result)) {
            return true;
        }
        return false;
    }

    public static String getJsonToDoubleString(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0.00" : formatDouble(StringConverters.ToDouble(money));
    }

    public static String getJsonToDouble3String(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0.000" : formatDouble3(StringConverters.ToDouble(money));
    }

    public static String getJsonToIntegerString(JSONArray ja, Integer index) {
        String money = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
        return StringUtils.isEmpty(money) ? "0" : money;
    }

    public static String getJsonToTimeString(JSONArray ja, Integer index) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, "yyyy-MM-dd HH:mm");
        } catch (Exception e) {
            return "";
        }
    }

    public static String getJsonToDateString(JSONArray ja, Integer index) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            return "";
        }
    }

    public static String getJsonToDateParamString(JSONArray ja, Integer index, String stampParam) {
        try {
            String timeStamp = "null".equals(ja.getString(index)) ? "" : ja.getString(index);
            String time = JSONObject.fromObject(timeStamp).getString("time");
            return StringUtils.isEmpty(time) ? "" : stampToDate(time, stampParam);
        } catch (Exception e) {
            return "";
        }
    }

    public static Integer formatDoubleToInteger(double d) {
        DecimalFormat df = new DecimalFormat("########0");
        return StringConverters.ToInteger(df.format(d));
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s, String formatParam) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatParam);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    //获取最后一个时间
    public static String getLastTime(Integer days) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -days);
        Date d = c.getTime();
        return DateTimeUtils.format(DateTimeUtils.getBeginOfDay(StringConverters.ToDatePattern(format.format(d), "yyyy-MM-dd HH:mm:ss")), "yyyy-MM-dd HH:mm:ss");
    }

    //获取几天前/几天后的时间
    public static String getBeforeTime(Integer days) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -days);
        Date d = c.getTime();
        Date d1 = StringConverters.ToDatePattern(format.format(d), "yyyy-MM-dd HH:mm:ss");
        return DateTimeUtils.formatDateToStringWithTime(d1);
    }
}

